package com.company.project.service.impl;

import com.company.project.dao.AuthorityMapper;
import com.company.project.dao.RoleAuthorityMapper;
import com.company.project.dao.RoleMapper;
import com.company.project.dao.UserRoleMapper;
import com.company.project.model.Authority;
import com.company.project.model.Role;
import com.company.project.model.RoleAuthority;
import com.company.project.model.UserRole;
import com.company.project.service.RoleAuthorityService;
import com.company.project.core.AbstractService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


/**
 * Created by CodeGenerator on 2018/10/08.
 */
@Service
@Transactional
public class RoleAuthorityServiceImpl extends AbstractService<RoleAuthority> implements RoleAuthorityService {
    @Resource
    private RoleAuthorityMapper roleAuthorityMapper;

    @Resource
    private RoleMapper roleMapper;

    @Resource
    private UserRoleMapper userRoleMapper;

    @Resource
    private AuthorityMapper authorityMapper;

    /**
     * 后台删除角色权限
     *
     * @return
     */
    @Override
    public Boolean deleteRoleAuthority(Integer rid) {
        Integer integer = userRoleMapper.selectURCount(rid);
        if (integer !=0){
            return false;
        }else {
            Boolean aBoolean = roleAuthorityMapper.deleteRoleAuthority(rid);
            Boolean aBoolean1 = roleMapper.deleteRole(rid);
            if (aBoolean1.equals(true) && aBoolean.equals(true)){
                return true;
            }
            return false;
        }
    }

    @Override
    public Boolean deleteRoleA(RoleAuthority roleAuthority) {
        Boolean aBoolean = roleAuthorityMapper.deleteRoleA(roleAuthority);
        Boolean aBoolean1 = this.insertRA(roleAuthority);
        if (aBoolean.equals(true) && aBoolean1.equals(true)){
            return true;
        }
        return false;
    }

    @Override
    public Boolean insertRA(RoleAuthority roleAuthority) {
        return roleAuthorityMapper.insertRA(roleAuthority);
    }

    /**
     * 用户权限添加
     *
     * @param roleAuthority
     * @return
     */
    @Override
    public Boolean addRoleAuthority(RoleAuthority roleAuthority) {
        Boolean aBoolean = roleMapper.addRole(roleAuthority);
        Boolean aBoolean2 = roleAuthorityMapper.addRoleAuthority(roleAuthority);
        if (aBoolean.equals(true)  && aBoolean2.equals(true)) {
            return true;
        }
        return false;
    }
}
